<template>
  <div class="editPage__video">
    <el-dialog
      class="uploadIMG"
      title="上传视频"
      :lock-scroll="true"
      :visible.sync="dialogVisible"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :show-close="false"
      width="500px"
      center>
      <div class="img__con">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="onSuccess"
          :before-upload="beforeUpload"
          :http-request="httpRequest"
          :on-change="videoChange"
          :before-remove="handleRemove"
          ref="uploadvideo">
          <video width="100%" v-if="dialogVideoUrl" controls="controls">
            <source :src="dialogVideoUrl" type="video/mp4" />
          </video>
          <!-- <img v-if="imageUrl" :src="imageUrl" class="avatar" /> -->
          <div class="uploadbtn" v-else>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将视频拖到此处，或<em>点击上传</em></div>
          </div>
        </el-upload>
        <p style="color:red">说明: 视频格式为mp4格式,每个视频大小不超过100M</p>
        <!-- 按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消 上 传</el-button>
          <!-- :disabled="disabl" -->
          <el-button type="primary" @click="uploadVideoSrc">点 击 上 传</el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
import { getDomain } from "@/api/basisMG";
export default {
  name: "UploadVideo",
  data() {
    return {
      signData: "",
      uploadData: {
        fileName: "223",
      },
      fileLists: [],
      dialogVisible: false, // 弹框默认隐藏
      dialogVideoUrl: "", // 上传的视频
      OssDomain: "",
      uploadShow: false,
    };
  },
  created() {
    this.getDomain();
  },
  methods: {
    //获取页面标识
    async getDomain() {
      const res = await getDomain("OSS");
      this.OssDomain =
        res.data && res.data.protocal ? res.data.protocal + "://" + res.data.host : "";
    },
    // 上传按钮
    videoChange(file, fileList) {
      this.fileLists = fileList;
      console.log("shan", fileList);
    },
    /* 显示上传文件组件 */
    showUpload() {
      this.dialogVisible = true;
      if (this.dialogVideoUrl !== '') {
        this.clearFiles();
        this.dialogVideoUrl = "";
      }
    },
    clearFiles () {
      this.$refs['uploadvideo'].clearFiles();
    },
    /* 传递视频地址 */
    uploadVideoSrc() {
      this.$refs.uploadvideo.submit();
      // 隐藏上传弹框
      this.dialogVisible = false;
      this.uploadShow = false;
    },
    beforeUpload(file) {
      return true;
    },
    httpRequest(files) {
      const file = files.file;
      const isMp4 = file.type === "video/mp4";
      const timestamp = new Date().getTime().toString().slice(0, 10);
      const infoData = {
        app: "nrms",
        areacode: "nrms",
        expire: 30,
      };
      if (!isMp4) {
        this.$message.error("视频只能是mp4格式!");
      } else {
        this.signUpload(file.uid, timestamp, infoData).then((res) => {
          this.signData = res.data.data;
          const json = {
            filetype: "V",
            rcode: `${file.uid}`,
            expire: 30,
            appid: "qm202006",
            areacode: "nrms",
            app: "nrms",
            isbackup: 0,
            sign: this.signData,
            timestamp: timestamp,
            uploadfile: files.file,
          };
          this.AUpload(json).then((res) => {
            console.log("上传", res);
            this.dialogVideoUrl = this.OssDomain + res.data.data.filePath;
            this.fileLists = [];
            this.$emit("uploadVideoSrc", this.dialogVideoUrl);
          });
        });
      }
    },
    signUpload(rcode, timestamp, infoData) {
      return axios.post("/gw/tqmsapi/sign/upload", {
        rcode,
        timestamp,
        ...infoData,
      });
    },
    AUpload(json) {
      console.log("json----", json);
      let formData = new FormData();
      for (var key in json) {
        formData.append(key, json[key]);
      }
      return axios({
        method: "post",
        url: "/appnass/v1/appupload",
        data: formData,
        headers: { "Content-Type": "multipart/form-data" },
      });
    },
    // 关闭弹框
    handleClose() {
      this.$confirm("点击取消后您填写的信息将丢失，您确定取消？")
        .then(() => {
          this.fileLists = [];
          this.handleRemove();
          this.$emit("handleClose");
          // 隐藏上传文件
          this.dialogVisible = false;
          this.dialogVideoUrl = "";
        })
        .catch(() => {});
    },
    // 删除视频
    handleRemove() {
      this.uploadShow = false;
      this.dialogVideoUrl = "";
      return true;
    },
    // 上传成功
    onSuccess(response) {
      console.log(response, "888888");
      // 返回错误
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../assets/css/mixin.scss";
.uploadbtn {
  width: 350px;
  height: 100px;
  line-height: 40px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  padding:20px 0;
  margin:20px auto;
}
.img__con{
  text-align:center;
}
.el-icon-upload {
  font-size: 40px;
  color: #8c939d;
  text-align: center;
}
</style>
